<?php
/**
 * Created by PhpStorm.
 * User: zhyg
 * Date: 2015/8/23
 * Time: 9:33
 * 给上传好的图片添加水印,可以在配置文件中定义一个文件夹专门用于保存水印图，制作水印方法在图片加上水印后同时返回水印的路径，用于保存在数据库中，
 */
class Image_water{
    private $image_water_path ;     //水印图（eg:logo）绝对位置，配置文件中取
    private $image_finish_folder = '';   //完成图保存文件夹名，配置文件中取
    private $image_finish_pre = '';       //完成图文件名前缀，配置文件中取
    private $image_origin_path;     //原图绝对位置
    private $image_origin_dir;     //原图绝对路径
    private $image_finish_dir;     //完成图保存绝对路径
    private $image_finish_path;     //完成图保存绝对位置
    private $image_finish_url;      //完成图保存在数据库中的值
    private $image_exts;
    private $water_position;
    private $water_pct;
    const LEFT_TOP=1;
    const RIGHT_TOP=2;
    const CENTER=3;
    const LEFT_BOTTOM=4;
    const RIGHT_BOTTOM=5;
    public $error;
    /**
     *
     * @param String $image_origin_url  原图在数据库中的值（原图相对位置）
     */
    public function __construct($image_origin_url){
        $image_origin_name = basename($image_origin_url);

        $this->image_origin_path = SITE_ROOT.DS.$image_origin_url;
        $this->image_origin_dir = dirname($this->image_origin_path);
        $this->image_water_path = $GLOBALS['config']['image_water']['image_water_path'];
        //echo $this->image_water_path ;
        $this->image_finish_folder = $GLOBALS['config']['image_water']['image_finish_folder'];
        $this->image_finish_pre = $GLOBALS['config']['image_water']['image_finish_pre'];
        $this->image_exts = $GLOBALS['config']['image_water']['image_exts'];
        $this->water_position = $GLOBALS['config']['image_water']['water_position'];
        $this->water_pct = $GLOBALS['config']['image_water']['water_pct'];
        $this->image_finish_dir =$this-> makeFinishDir($this->image_finish_folder);
        $this->image_finish_path = $this->image_finish_dir.DS.$this->image_finish_pre.$image_origin_name;
        $this->image_finish_url = strrchr($this->image_finish_path,'Upload');
    }

    /**
     * @param String $image_finish_folder ：自定义缩略图文件夹名，如果为空，则和原图存在同一目录
     * @return String $image_finish_dir：完成图保存绝对路径
     */
    private function makeFinishDir($image_finish_folder){
        if(empty($image_finish_folder)){
            $image_finish_dir = $this->image_origin_dir;
        }else{
            $image_finish_dir = $this->image_origin_dir.DS.$image_finish_folder;
            if(!is_dir($image_finish_dir)){
                mkdir($image_finish_dir);
            }
        }
        return $image_finish_dir;
    }
    private function checkImageOrigin($image_origin_path){
        if(!is_file($image_origin_path)){
            $this->error = "该文件不存在";
            return false;
        }
        if(strpos($image_origin_path,UPLOAD_DIR)===false){
            $this->error = "该文件存在,但不是在Upload目录下";
            return false;
        }
        //如果图片有效，则返回后缀名
        $ext =  substr($image_origin_path,strrpos($image_origin_path,'.')+1);
        if(strpos($this->image_exts,$ext)===false){
            $this->error = "图片类型不正确，应为$this->image_exts";
            return false;
        }
        if($ext=='jpg')$ext='jpeg';
        return $ext;

    }
    public function makeWater(){
        $ext_orign = $this->checkImageOrigin($this->image_origin_path);
        if(!$ext_orign){
            return  false;
        }
        $ext_water = $this->checkImageOrigin($this->image_water_path);
        if(!$ext_water){
            return  false;
        }

        $image_origin_resource_func = "imagecreatefrom".$ext_orign;
        $image_water_resource_func = "imagecreatefrom".$ext_water;
        $image_save_func = "image".$ext_orign;
        $image_origin_resource = $image_origin_resource_func($this->image_origin_path);
        $image_water_resource = $image_water_resource_func($this->image_water_path);
        $image_origin_info = getimagesize($this->image_origin_path);
        $image_water_info = getimagesize($this->image_water_path);
        switch($this->water_position){
            case self::LEFT_TOP:
                $pos_x = 0;
                $pos_y = 0;
                break;
            case self::RIGHT_TOP:
                $pos_x = $image_origin_info[0] - $image_water_info[0];
                $pos_y = 0;
                break;
            case self::CENTER:
                $pos_x = ceil(($image_origin_info[0] - $image_water_info[0])/2);
                $pos_y = ceil(($image_origin_info[1] - $image_water_info[1])/2);
                break;
            case self::LEFT_BOTTOM:
                $pos_x = 0;
                $pos_y = $image_origin_info[1] - $image_water_info[1];
                break;
            case self::RIGHT_BOTTOM:
                $pos_x = $image_origin_info[0] - $image_water_info[0];
                $pos_y = $image_origin_info[1] - $image_water_info[1];
                break;
        }
       imagecopymerge($image_origin_resource,$image_water_resource,$pos_x,$pos_y,0,0,$image_water_info[0],$image_water_info[1],$this->water_pct);
        if($image_save_func($image_origin_resource,$this->image_finish_path)){
            return $this->image_finish_url;
        }else{
            $this->error = "图片制作失败，系统原因";
            return false;
        }
    }
}